package com.java8.stream.dome;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class Test3 {
    public static void master() {
        System.out.println("-----------------Test3.master()-----------------");

        //1.1 去空
        List<String> list = Arrays.asList("悟空", "", "唐僧", "八戒", "悟净", "白龙马");
        List<String> newList = list.stream().filter(str -> !str.isEmpty()).collect(Collectors.toList());
        //结果：newList=("悟空","唐僧","八戒","悟净","白龙马")
        System.out.println("1.1 去空");
        System.out.println(list);
        System.out.println(newList);

        //1.2 筛选
        Integer[] nums = {1, 2, 3, 4, 5, 6};
        Integer[] evenNums = Stream.of(nums).filter(n -> n % 2 == 0).toArray(Integer[]::new);
        //结果：evenNums=[2, 4, 6]
        System.out.println("1.2 筛选");
        System.out.println(Arrays.toString(nums));
        System.out.println(Arrays.toString(evenNums));

        // 1.3 计数
        long count = Stream.of(nums).filter(n -> n % 2 == 0).count();
        //结果：count=3
        System.out.println("1.3 计数");
        System.out.println(Arrays.toString(evenNums));
        System.out.println("count = " + count);

        //1.4 distinct ()：返回集合中各异的元素集合(去重)
        Integer[] numbers = {5, 1, 2, 1, 3, 3, 2, 4};
        Integer[] newNumbers = Arrays.stream(numbers).distinct().toArray(Integer[]::new);
        //结果：newNumbers =  [5, 1, 2, 3, 4]
        System.out.println("1.4 distinct ()：返回集合中各异的元素集合(去重)");
        System.out.println(Arrays.toString(numbers));
        System.out.println(Arrays.toString(newNumbers));
    }
}
